package com.ross.web.maintenance;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.ross.web.dao.CategoryDAO;
import com.ross.web.domain.Category;
import com.ross.web.util.SessionLogout;

public class CategoryAction extends ActionSupport {
	
	private int dispMode;
	private List<Category> categoryList;
	private Category category;
	
	private SessionLogout sessionLogout = new SessionLogout();
	private HttpServletRequest request = ServletActionContext.getRequest();
	
	CategoryDAO categoryDAO = new CategoryDAO();
	
	public String goToCategoryAction(){
		
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		categoryList = categoryDAO.getAllCategory();
		setDispMode(1);
		return SUCCESS;
	}
	
	public String addCategoryAction(){

		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try {
			
			if(!validateInputs()){
				categoryList = categoryDAO.getAllCategory();
				setDispMode(1);
				return ERROR;
			}else{
				categoryDAO.insert(category);
				addActionMessage(getText("success.category.insert"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		categoryList = categoryDAO.getAllCategory();
		setDispMode(1);
		return SUCCESS;
	}
	
	public String goToUpdateCategoryAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		category = categoryDAO.getAllCategoryById(category.getC_id());
		
		setDispMode(2);
		return SUCCESS;
	}
	
	public String updateCategoryAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try {
			
			if(!validateInputs()){
				setDispMode(2);
				return ERROR;
			}else{
				categoryDAO.update(category);
				addActionMessage(getText("success.category.update"));
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		categoryList = categoryDAO.getAllCategory();
		setDispMode(1);
		return SUCCESS;
	}
	
	public String deleteCategoryAction(){
		if (sessionLogout.SessionExpired()) {
			addActionMessage("Session already expired");
			return "sessionExpired";
		}
		
		try {
			
			categoryDAO.delete(category);
			addActionMessage(getText("success.category.delete"));
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		setDispMode(1);
		categoryList = categoryDAO.getAllCategory();
		return SUCCESS;
	}
	
	private boolean validateInputs(){
		
		if(category.getC_name().trim().length() == 0 || category.getC_name() == null){
			addActionError(getText("error.category.namerequired"));
		}else if(category.getC_name().length() >= 50){
			addActionError(getText("error.category.name_exceed"));
		}
		
		if(category.getC_description().trim().length() == 0 || category.getC_description() == null){
			addActionError(getText("error.category.descriptionrequired"));
		}else if(category.getC_description().length() >= 50){
			addActionError(getText("error.category.description_exceed"));
		}
		
		if(getDispMode() == 1){
			if(categoryDAO.getCategoryByName(category.getC_name()) != null){
				addActionError(getText("error.category.nameexist"));
			}
		}else if(getDispMode() == 2){
			if(categoryDAO.getCategoryByName(category.getC_name()) != null){
				Category c = new Category();
				c = categoryDAO.getCategoryByName(category.getC_name());
				if(c.getC_id() != category.getC_id()){
					addActionError(getText("error.category.nameexist"));
				}
			}
		}
		
		if(this.hasActionErrors()){
			return false;
		}else{
			return true;
		}
	}
	
	

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public int getDispMode() {
		return dispMode;
	}

	public void setDispMode(int dispMode) {
		this.dispMode = dispMode;
	}
	
	

}
